#pragma once
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <string>

namespace ns_task
{
    class Task
    {
    public:
        Task()
            : _sock(-1)
        {
        }

        Task(int sock)
            :_sock(sock)
        {
        }

        ~Task()
        {
        }

        int operator()()
        {
            return Run();
        }

    public:
        int Run()
        {
            char buffer[1024];
            memset(buffer, 0, sizeof(buffer) - 1);
            ssize_t cnt = read(_sock, buffer, sizeof(buffer) - 1);
            if (cnt > 0)
            {
                buffer[cnt] = 0;
                std::cout << "client say# " << buffer << std::endl;
                std::string s;
                s += "server ";
                s += buffer;
                write(_sock, s.c_str(), s.size());
            }
            else if (cnt == 0)
            {
                std::cout << "client quit..." << std::endl;
            }
            else
            {
                std::cerr << "read error" << std::endl;
            }
            close(_sock);
        }

    private:
        int _sock;
    };
}
